package com.maiji.cloud.util;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.IplImage;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class VideoImage {

    public static final String IMAGEMAT = "png";
    private static final String ROTATE = "rotate";

    /**
     * 默认截取视频的中间帧为封面
     */
    public static final int MOD = 2;

    public static void main(String[] args) throws Exception {

    }

    /**
     * 获取视频缩略图
     * @param inputStream：inputStream
     * @param mod：视频长度/mod获取第几帧
     * @throws Exception
     */
    public static BufferedImage randomGrabberFFmpegImage(InputStream inputStream, int mod) throws Exception {
        BufferedImage bufferedImage;
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(inputStream);
        ff.start();
        String rotate = ff.getVideoMetadata(ROTATE);
        int ffLength = ff.getLengthInFrames();
        Frame f;
        int i = 0;
        int index = ffLength / mod;
        ff.setFrameNumber(mod);
        f = ff.grabImage();
        bufferedImage = doExecuteFrame(f);

        ff.stop();
        return bufferedImage;
    }

    /**
     * 根据视频路径生成缩略图存放路径
     * @param filePath：视频路径
     * @param index：第几帧
     * @return：缩略图的存放路径
     */
    private static String getImagePath(String filePath, int index){
        if(filePath.contains(".") && filePath.lastIndexOf(".") < filePath.length() - 1){
            filePath = filePath.substring(0, filePath.lastIndexOf(".")).concat("_").concat(String.valueOf(index)).concat(".").concat(IMAGEMAT);
        }
        return filePath;
    }

    /**
     * 旋转图片
     * @param src
     * @param angle
     * @return
     */
    public static IplImage rotate(IplImage src, int angle) {
        IplImage img = IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
        opencv_core.cvTranspose(src, img);
        opencv_core.cvFlip(img, img, angle);
        return img;
    }

    /**
     * 截取缩略图
     * @param f
     * @param targerFilePath:封面图片
     */
    public static void doExecuteFrame(Frame f, String targerFilePath) {
        if (null == f || null == f.image) {
            return;
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bi = converter.getBufferedImage(f);
        File output = new File(targerFilePath);
        try {
            ImageIO.write(bi, IMAGEMAT, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 截取缩略图
     * @param f
     * @param
     */
    public static BufferedImage doExecuteFrame(Frame f) {
        if (null == f || null == f.image) {
            return null;
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bi = converter.getBufferedImage(f);
        return bi;
    }

}
